home *** CD-ROM | disk | FTP | other *** search
- /* DumpLog.c */
- /*
- * DumpLog.c
- * Copyright © 1992-95 Apple Computer Inc. All Rights Reserved.
- *
- * Edit History
- */
- #include "DumpLog.h"
- #include <stdio.h>
-
- #ifndef TRUE
- #define TRUE 1
- #define FALSE 0
- #endif
-
- enum {
- NUL = 0,
- kOpenQuote = 0xD2,
- kCloseQuote = 0xD3
- };
- #define kLogLinkFailed ((UniversalProcPtr) (-1L))
-
- void DoAllLogRecords(void);
- void DoThisLogRecord(
- LogRecordPtr logRecordPtr
- );
- void DoLogEntryRange(
- LogRecordPtr logRecordPtr,
- UInt32 startIndex,
- UInt32 endIndex
- );
- void ConvertTimestamp(
- LogEntryPtr logEntryPtr,
- DateTimeRec *eventDateTime,
- UInt32 *residualNanoseconds
- );
-
-
- #define AppendChar(result, theChar) do { \
- StringPtr _dst = (result); \
- _dst[++_dst[0] & 0xFF] = theChar; \
- } while (0)
- void PutUnsignedLeadingZeros(
- UInt32 value,
- short digits,
- unsigned char terminator
- );
- void PutHexLeadingZeros(
- UInt32 value,
- short digits
- );
- void PutUnsigned(
- UInt32 value
- );
- void PutSigned(
- SInt32 value
- );
- void PutUnsignedInField(
- UInt32 value,
- short fieldWidth
- );
- void PutChar(
- unsigned char datum
- );
- void PutPascalString(
- ConstStr255Param datum
- );
- void PutCString(
- const char *datum
- );
- void PutLine(void);
-
- LogRecordPtr FindLogRecordPtr(
- StringPtr logRecordName /* Log name (P-string) */
- );
-
- /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
- * WriteAllLogRecords
- *
- * Enumerate the Log Record entries and dump any new information.
- */
- void
- WriteAllLogRecords(void)
- {
- OSErr status;
- LogRecordIter cookie;
- LogRecordPtr logRecordPtr;
-
- /*
- * Second pass does the work.
- */
- status = LogRecordIterateCreate(&cookie);
- if (status == noErr) {
- while ((logRecordPtr = LogRecordIterate(&cookie)) != NULL)
- WriteThisLogRecord(logRecordPtr);
- LogRecordIterateDispose(&cookie);
- }
-
- }
-
- /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
- * WriteThisLogRecord
- *
- * Enumerate the Log Record entries and dump any new information.
- */
- void
- WriteThisLogRecord(
- LogRecordPtr logRecordPtr
- )
- {
- LogEntryRecord thisLogEntry;
- static LogRecordPtr lastLogRecordPtr;
- short limitCount;
-
- limitCount = 0;
- while (ReadLogEntry(logRecordPtr, &thisLogEntry) == noErr) {
- if (lastLogRecordPtr != logRecordPtr) {
- PutPascalString("\p ");
- PutCString((const char *) logRecordPtr->logName);
- PutLine();
- lastLogRecordPtr = logRecordPtr;
- }
- DoThisLogEntry(&thisLogEntry);
- if (++limitCount >= 512 || Button()) /* Always process events */
- break;
- }
- }
-
- /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
- * DoAllLogRecords
- *
- * Enumerate through the Log Record entries in the Name Registry.
- */
- void
- DoAllLogRecords(void)
- {
- OSErr status;
- LogRecordIter cookie;
- LogRecordPtr logRecordPtr;
- UInt32 count;
-
- /*
- * First pass just lists the records.
- */
- status = LogRecordIterateCreate(&cookie);
- count = 0;
- while ((logRecordPtr = LogRecordIterate(&cookie)) != NULL) {
- ++count;
- PutPascalString("\p ");
- PutCString((const char *) logRecordPtr->logName);
- PutPascalString("\p at ");
- PutHexLeadingZeros((UInt32) logRecordPtr, 8);
- PutLine();
- }
- LogRecordIterateDispose(&cookie);
- PutUnsigned(count);
- PutPascalString(
- (count == 1) ? "\p LogRecord found" : "\p LogRecords found"
- );
- PutLine();
- /*
- * Second pass does the work.
- */
- status = LogRecordIterateCreate(&cookie);
- if (status == noErr) {
- while ((logRecordPtr = LogRecordIterate(&cookie)) != NULL)
- DoThisLogRecord(logRecordPtr);
- LogRecordIterateDispose(&cookie);
- }
- }
-
- /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
- * DoThisLogRecord
- *
- * We have a LogRecord. Fetch a copy of the static part of the record, display the
- * fixed information, then display all LogEntries.
- */
- void
- DoThisLogRecord(
- LogRecordPtr logRecordPtr
- )
- {
- LogRecord logRecordCopy;
- OSErr status;
- #define LOG (logRecordCopy)
-
- if (logRecordPtr != NULL) {
- PutPascalString("\p• LogRecord \"");
- PutCString((const char *) logRecordPtr->logName);
- PutPascalString("\p\", at ");
- PutHexLeadingZeros((UInt32) logRecordPtr, 8);
- PutLine();
- status = CopyLogRecordInfo(logRecordPtr, &logRecordCopy);
- if (status != noErr) {
- PutPascalString("\pCould not copy log record: ");
- PutSigned(status);
- PutLine();
- }
- else {
- if (LOG.semaphore != 0) {
- PutPascalString("\p ••• Semaphore locked •••\n");
- PutLine();
- }
- if (LOG.lostLockCounter != 0) {
- PutPascalString("\p Lost Semaphore Counter ");
- PutUnsigned(LOG.lostLockCounter);
- PutLine();
- }
- PutPascalString("\p Sequence Counter ");
- PutUnsigned(LOG.sequenceCounter);
- PutPascalString("\p, Flags = ");
- PutHexLeadingZeros(LOG.flags, 8);
- PutPascalString("\p, ");
- PutPascalString(
- ((LOG.flags & kLogDataEnabledMask) != 0)
- ? "\pEnabled"
- : "\pDisabled"
- );
- PutPascalString("\p, Preserve ");
- PutPascalString(
- ((LOG.flags & kLogDataPreserveFirstMask) != 0)
- ? "\pearliest."
- : "\pmost recent."
- );
- PutLine();
- if ((LOG.flags & kLogDataWrapAroundMask) != 0) {
- if (LOG.entryGetIndex < LOG.entryPutIndex) {
- /*
- * +------------+
- * | |
- * | Get Index | Do Put+1 to End (already displayed)
- * | | Do Start to Get (already displayed)
- * | Put Index | Do Get+1 to Put (yet to be displayed)
- * | |
- * +------------+
- */
- DoLogEntryRange( /* Already displayed */
- logRecordPtr,
- LOG.entryPutIndex + 1,
- LOG.entryMaxIndex - 1
- );
- DoLogEntryRange( /* Already displayed */
- logRecordPtr,
- 0,
- LOG.entryGetIndex
- );
- DoLogEntryRange( /* Not yet displayed */
- logRecordPtr,
- LOG.entryGetIndex + 1,
- LOG.entryPutIndex
- );
- }
- else {
- /*
- * +------------+
- * | |
- * | Put Index | Do Put+1 to Get (already displayed)
- * | | Do Get+1 to End (yet to be displayed)
- * | Get Index | Do Start to Put (yet to be displayed)
- * | |
- * +------------+
- */
- DoLogEntryRange( /* Already displayed */
- logRecordPtr,
- LOG.entryPutIndex + 1,
- LOG.entryGetIndex
- );
- DoLogEntryRange( /* Not yet displayed */
- logRecordPtr,
- LOG.entryGetIndex + 1,
- LOG.entryMaxIndex - 1
- );
- DoLogEntryRange( /* Not yet displayed */
- logRecordPtr,
- 0,
- LOG.entryPutIndex
- );
- }
- }
- else {
- /*
- * We haven't wrapped around yet.
- * +------------+
- * | 0 | Untouched
- * | 1 | First real entry
- * | |
- * | Get Index |
- * | | Do Start to Get (already displayed)
- * | Put Index | Do Get+1 to Put (yet to be displayed)
- * | |
- * +------------+
- */
- DoLogEntryRange( /* Already displayed */
- logRecordPtr,
- 1,
- LOG.entryGetIndex
- );
- DoLogEntryRange( /* Not yet displayed */
- logRecordPtr,
- LOG.entryGetIndex + 1,
- LOG.entryPutIndex
- );
- }
- PutPascalString("\p• End of \"");
- PutCString((const char *) &LOG.logName);
- PutPascalString("\p\"");
- PutLine();
- }
- }
- }
-
- /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
- * DoLogEntryRange
- *
- * Process a range of log record entries. Note: the range is inclusive
- */
- void
- DoLogEntryRange(
- LogRecordPtr logRecordPtr,
- UInt32 startIndex,
- UInt32 endIndex
- )
- {
- LogEntryRecord thisLogEntry;
- OSErr status;
-
- while (startIndex <= endIndex) {
- status = CopyLogEntry(
- logRecordPtr, startIndex, &thisLogEntry);
- if (status == noErr)
- DoThisLogEntry(&thisLogEntry);
- else if (status == fBsyErr) {
- PutPascalString("\p Semaphore Locked");
- PutLine();
- break;
- }
- else {
- PutSigned(status);
- PutPascalString("\p: strange status");
- PutLine();
- break;
- }
- ++startIndex;
- }
- }
-
-
- /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
- * DoThisLogEntry
- *
- * Display a single log entry.
- */
- void
- DoThisLogEntry(
- LogEntryPtr logEntryPtr
- )
- {
- DateTimeRec eventDateTime;
- UInt32 residualNanoseconds;
- Str255 work;
- #define ENTRY (*logEntryPtr)
-
- /*
- * This is what we've been waiting for. We compute the timestamp
- * using a native-only algorithm to get better accuracy than we
- * could get using the 68000-compatible algorithm.
- */
- LogConvertTimestamp(logEntryPtr, &eventDateTime, &residualNanoseconds);
- PutUnsignedInField(ENTRY.sequence, 5);
- PutPascalString("\p ");
- PutUnsignedLeadingZeros(eventDateTime.hour, 2, ':');
- PutUnsignedLeadingZeros(eventDateTime.minute, 2, ':');
- PutUnsignedLeadingZeros(eventDateTime.second, 2, '.');
- PutUnsignedLeadingZeros(residualNanoseconds / 1000L, 6, NUL); /* uSec */
- FormatLogEntryData(&ENTRY, work);
- PutPascalString("\p ");
- PutPascalString(work);
- PutLine();
- #undef ENTRY
- }
-
- /*
- * Output an n-digit value with leading zeros.
- */
- void
- PutUnsignedLeadingZeros(
- UInt32 value,
- short digits,
- unsigned char terminator
- )
- {
- if (--digits > 0)
- PutUnsignedLeadingZeros(value / 10, digits, NUL);
- PutChar((value % 10) + '0');
- if (terminator != NUL)
- PutChar(terminator);
- }
-
- /*
- * Output a signed decimal longword.
- */
- void
- PutSigned(
- SInt32 value
- )
- {
- if (value < 0) {
- PutChar('-');
- value = (-value);
- }
- PutUnsigned((unsigned long) value);
- }
-
- /*
- * Output an unsigned decimal longword.
- */
- void
- PutUnsigned(
- UInt32 value
- )
- {
- if (value >= 10)
- PutUnsigned(value / 10);
- PutChar((value % 10) + '0');
- }
-
- /*
- * Output a string of hex digits with leading zeros.
- */
- void
- PutHexLeadingZeros(
- unsigned long value,
- short digits
- )
- {
- if (--digits > 0)
- PutHexLeadingZeros(value >> 4, digits);
- value &= 0x0F;
- PutChar((value < 10)
- ? value + '0'
- : (value + ('A' - 10))
- );
- }
-
- /*
- * Output an n-digit decimal value with leading blanks.
- */
- void
- PutUnsignedInField(
- UInt32 value,
- short fieldWidth
- )
- {
- Str15 work;
-
- NumToString(value, work);
- while (work[0] < fieldWidth) {
- PutChar(' ');
- --fieldWidth;
- }
- PutPascalString(work);
- }
-
- void
- PutPascalString(
- ConstStr255Param datum
- )
- {
- DumpDrawString(datum);
- }
-
- void
- PutCString(
- const char *datum
- )
- {
- char *cp;
-
- for (cp = (char *) datum; *cp != NUL; cp++)
- PutChar(*cp);
- }
-
- void
- PutChar(
- unsigned char datum
- )
- {
- char work[1];
-
- if (datum == '\n')
- PutLine();
- else {
- work[0] = datum;
- DumpDrawText((const char *) &work[0], sizeof (char));
- }
- }
-
-
- void
- PutLine(void)
- {
- DumpDrawLine("\p");
- }
-
-
-